#lang racket

(require "rename-mutable-pair-ops.rkt")

(provide lookup insert! make-table print-table)

(define (make-table)
  (list '*table*))

(define (assoc key records)
  (cond ((null? records) false)
        ((equal? key (car (car records))) (car records))
        (else (assoc key (cdr records)))))

(define (lookup key-1 key-2 table)
  (let ((subtable (assoc key-1 (cdr table))))
    (if subtable
        (let ((record (assoc key-2 (cdr subtable))))
          (if record
              (cdr record)
              false))
        false)))

(define (insert! key-1 key-2 value table)
  (let ((subtable (assoc key-1 (cdr table))))
    (if subtable
        (let ((record (assoc key-2 (cdr subtable))))
          (if record
              (set-cdr! record value)
              (set-cdr! subtable
                        (cons (cons key-2 value)
                              (cdr subtable)))))
        (set-cdr! table
                  (cons (list key-1
                              (cons key-2 value))
                        (cdr table))))))


(define (print-1d-table t)
  (print-list (cdr t)))

(define (print-table t)
  (define (iter subt)
    (cond ((not (null? subt)) (display (car (car subt)))
                              (display ": ")
                              (print-1d-table (car subt))
                              (iter (cdr subt)))))
  (display "[\n")
  (iter (cdr t))
  (display "]"))